package org.iteam.xlz.manager.action.product;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;
import org.iteam.xlz.core.entity.product.Product;
import org.iteam.xlz.core.service.product.ProductService;
import org.iteam.xlz.core.service.product.ProductTypeService;

import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("unchecked")
@Results({@Result(name="INDEX",type="freemarker",location="/WEB-INF/content/product/info/index.html"),
	      @Result(name="EDIT",type="freemarker",location="/WEB-INF/content/product/info/edit.html")})
public class ProductInfoAction extends AbstractModuleAction<Product> implements RestAction,ModelDriven<Product>{


	/**
	 * 
	 */
	private static final long serialVersionUID = 6618210202073879485L;
	private final String PATH="/product/info";
	
	@Override
	public Product getModel() {
		if(null==model){
			model=new Product();
		}
		return model;
	}	
	@Override
	@Action(PATH+"/index")
	public String index() {
		map.put("typeTree",ProductTypeService.getInstance().getTypeTree());
		return INDEX;
	}

	@Override
	@Action(PATH+"/query")
	public String query() {
		map.put("total",ProductService.getInstance().counts(model.getProductname(),model.getPid(),model.getPtid(),parent,leftCreateTime,rightCreateTime));
		map.put("rows",ProductService.getInstance().query(model.getProductname(),model.getPid(),model.getPtid(),parent,leftCreateTime,rightCreateTime,page,rows));
		return MAP;
	}

	@Override
	@Action(PATH+"/create")
	public String create() {
		model.setCreator(ShiroUtil.currentAccount());
		map.put("state",ProductService.getInstance().create(model));
		return MAP;
	}

	@Override
	@Action(PATH+"/update")
	public String update() {
		map.put("state",ProductService.getInstance().update(model));
		return MAP;
	}

	@Override
	@Action(PATH+"/{_id}/delete")
	public String delete() {
		map.put("state",ProductService.getInstance().delete(_id));
		return MAP;
	}

	@Override
	@Action(PATH+"/validate")
	public String destroy() {
		print(ProductService.getInstance().validate(model.getPid()));
		return null;
	}

	@Override
	@Action(PATH+"/{_id}/edit")
	public String edit() {
		object=ProductService.getInstance().findOne(_id);
		Product p=(Product)object;
		map.put("typeTree",ProductTypeService.getInstance().getTypeTree());
		map.put("typeparent",ProductTypeService.getInstance().getTypeParent(p.getPtid()));
		return EDIT;
	}

	@Override
	@Action(PATH+"/new")
	public String editNew() {
		map.put("typeTree",ProductTypeService.getInstance().getTypeTree());
		map.put("typeparent",ProductTypeService.getInstance().getTypeParent(null));
		return EDIT;
	}
	@Override
	public String show() {
		return null;
	}
	
	private String parent;
	private String leftCreateTime;
	private String rightCreateTime;

	public void setParent(String parent) {
		this.parent = parent;
	}
	public void setLeftCreateTime(String leftCreateTime) {
		this.leftCreateTime = leftCreateTime;
	}
	public void setRightCreateTime(String rightCreateTime) {
		this.rightCreateTime = rightCreateTime;
	}
}
